using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Universum.Views.Blogs;
using Universum.Model.Services.Blog;
using Universum.Infrastructure.Persistence;
using System.Security.Principal;
using TBlog = Universum.Model.Domain.Blogs.Blog;
using Universum.Infrastructure.Configuration;
namespace Universum.Controllers
{
	[Authorize(Roles = ConstantIdentifiers.ROLE_OF_TEACHER_OR_STUDENT)]
	public class BlogsController : UniversumController
	{
		IBlogService _Service;
		public BlogsController(IBlogService service,
			//IRepository _Repository,
			IPrincipal principal)
			: base(principal)
		{
			_Service = service;

		}
		//
		// GET: /Blog/

		public ActionResult Index()
		{
			var viewModel = new BlogIndexViewModel();

			viewModel.Blog = _Service.GetBlogByOwner(UsernameInRequest);
			viewModel.Username = CurrentUsername;
			viewModel.NavBarModel = new BlogNavBarViewModel();
			viewModel.NavBarModel.ItemsCount = 5;
			viewModel.NavBarModel.OtherBlogs = _Service.GetLatestBlogs(viewModel.NavBarModel.ItemsCount);
			viewModel.NavBarModel.RecentArticles = _Service.GetLatestArticles(viewModel.NavBarModel.ItemsCount);
			ViewData.Model = viewModel;

			if (viewModel.Blog != null) return RedirectToAction<BlogsController>(controller => controller.Show(0));

			return View();
		}

		public ActionResult Show(int id)
		{

			var viewModel = new BlogShowViewModel();
			viewModel.Username = CurrentUsername;
			viewModel.Blog = _Service.GetBlogByOwner(UsernameInRequest);

			viewModel.ArticlesCount = Math.Max(0, _Service.GetBlogArticlesCountFor(viewModel.Blog.ID));

			viewModel.StartIndex = Math.Max(id, 1);
			viewModel.ArticlesPerPage = ConstantIdentifiers.RESULTS_PER_PAGE;
			if(CurrentUsername == UsernameInRequest)
			viewModel.ArticlesForBlog = _Service.GetBlogArticlesPage

				(viewModel.Blog.ID,
				viewModel.StartIndex - 1,
				viewModel.ArticlesPerPage);
			else
				viewModel.ArticlesForBlog = _Service.GetPublishedBlogArticlesPage

				(viewModel.Blog.ID,
				viewModel.StartIndex - 1,
				viewModel.ArticlesPerPage);
			
			viewModel.NavBarModel = new BlogNavBarViewModel();
			viewModel.NavBarModel.ItemsCount = 5;
			viewModel.NavBarModel.OtherBlogs = _Service.GetLatestBlogs(viewModel.NavBarModel.ItemsCount);
			viewModel.NavBarModel.RecentArticles = _Service.GetLatestArticles(viewModel.NavBarModel.ItemsCount);
			ViewData.Model = viewModel;

			return View("Show");
		}


		public ActionResult Details()
		{
			var blog = _Service.GetBlogByOwner(this.UsernameInRequest);
			if (blog.IsNull())
				return RedirectToAction<BlogsController>(
				 c => c.Index());
			ViewData.Model = blog;
			return View();
		}

		public ActionResult Edit()
		{

			TBlog blog = _Service.GetBlogByOwner(CurrentUsername);
			if (blog.IsNotNull())
			{
				ViewData.Model = blog;
				return View();
			}
			return RedirectToAction<BlogsController>(controller => controller.Index());
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create()
		{
			var blog = _Service.GetBlogByOwner(CurrentUsername);
			if (blog.IsNull())
				_Service.CreateBlog(CurrentUsername);
			return RedirectToAction<BlogsController>(controller => controller.Edit());
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Remove()
		{
			TBlog blog = _Service.GetBlogByOwner(CurrentUsername);
			if (blog.IsNotNull())
			{
				_Service.RemoveBlog(blog);
			}
			return RedirectToAction<BlogsController>(controller => controller.Index());
		}

		[AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
		public ActionResult Save(
			[Bind(Exclude = "id, owner")]
			TBlog blog)
		{
			var theRealBlog = _Service.GetBlogByOwner(CurrentUsername);
			theRealBlog.Description = blog.Description;
			theRealBlog.Name = blog.Name;
			_Service.SaveOrUpdate(theRealBlog);
			return RedirectToAction<BlogsController>(controller => controller.Details());
		}

		public ActionResult ShowAll()
		{
			var model = new BlogShowAllViewModel();
			model.UsersWithBlogs = _Service.GetUsersWithBlogs();
			ViewData.Model = model;
			return View();
		}

	}
}
